package com.dinim.test.objectlibrary;


import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import com.dinim.graphics.IDrawable;
import com.dinim.matrix.twodimensional.IPoint2D;
import com.dinim.object.IObject2D;
import com.dinim.object.ITransformable;
import com.dinim.object.Rectangle2D;
import com.dinim.object.Star2D;
import com.dinim.physics2D.IBoundingBox2D;
import com.dinim.test.AbstractTest;

public class OBBContactResolver2Tester2 extends AbstractTest{

	static boolean run = false;
    static double direction = 1;
	
	public OBBContactResolver2Tester2(int width, int height) {
		super(width, height);
	}

	public static void main(String[] args) {
		OBBContactResolver2Tester2 crt = new OBBContactResolver2Tester2(400,400);
        
        crt.frame.addKeyListener(new KeyListener(){

            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                if(e.getKeyText(e.getKeyCode()).equals("Links")){
                    direction = -1;
                }
                if(e.getKeyText(e.getKeyCode()).equals("Rechts")){
                    direction = 1;
                }
                run = true;
            }

            public void keyReleased(KeyEvent e) {
                run = false;
            }

            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
                
            }
            
        });
        
		crt.frame.addMouseListener(new MouseListener(){

			public void mouseClicked(MouseEvent arg0) {
				run = !run;
			}

			public void mouseEntered(MouseEvent arg0) {

			}

			public void mouseExited(MouseEvent arg0) {

			}

			public void mousePressed(MouseEvent arg0) {

			}

			public void mouseReleased(MouseEvent arg0) {

			}
			
		});
		//crt.objects.add(new Rectangle2D(60, 60, 60, 300));
        crt.objects.add(new Rectangle2D(10, 160, 300, 100));
        //crt.objects.add(new Star2D(60, 60, 300, 300, 10));
        //((ITransformable)crt.objects.get(0)).rotate(1*direction, ((IObject2D)crt.objects.get(0)).getCenterPoint());
		//crt.objects.add(new Rectangle2D(250, 60, 400, 250));
        crt.objects.add(new Rectangle2D(300, 10, 300, 500));
		//crt.addRandomObjects(1);
        
		ObjectIntersectionResolver oir = new ObjectIntersectionResolver();
		
		while(true){
			for(int yield = 0; yield < 10000; yield++){
				Thread.yield();
			}
			while(run){
				
				crt.refreshCanvas();
				
				((ITransformable)crt.objects.get(0)).translate(1.0*direction, 0);
				((ITransformable)crt.objects.get(0)).rotate(3*direction, ((IObject2D)crt.objects.get(0)).getCenterPoint());
				((ITransformable)crt.objects.get(1)).translate(-2.0*direction, 0);
				((ITransformable)crt.objects.get(1)).rotate(-1*direction, ((IObject2D)crt.objects.get(1)).getCenterPoint());
                //crt.rotateAllObjectsConstantly(1);
                
                crt.startStopWatch(crt.DEFAULT_STOPWATCH);
				List intersections = oir.getIntersections(crt.objects);	
                crt.stopStopWatch(crt.DEFAULT_STOPWATCH);
                crt.printStopWatchTime(crt.DEFAULT_STOPWATCH);
                
				for(int i = 0; i < intersections.size(); i++){
					IPoint2D p = (IPoint2D) intersections.get(i);
					double width = 4;
	                double x1 = p.getX()-width;
	                double y1 = p.getY()-width;
	                double x2 = p.getX()+width;
	                double y2 = p.getY()+width;
	                crt.gc.drawOval((int)Math.round(x1), (int)Math.round(y1), (int)Math.round(x2), (int)Math.round(y2));
				}
				
				List bbintersections = oir.getBBIntersections(crt.objects);			
				
				for(int i = 0; i < bbintersections.size(); i++){
					IBoundingBox2D p = (IBoundingBox2D) bbintersections.get(i);
					crt.gc.setColor(Color.red);
					//((IDrawable)p).draw(crt.gc);
				}
				
				crt.drawAllObjects();
				//crt.drawAllObjectLineBoundingBoxes();
				crt.paint();
				
				for(int yield = 0; yield < 200000; yield++){
					Thread.yield();
				}
			}
		}
			
		
		
	}
	
}
